package com.atzongmaotan.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * ClassName:    BasicMappedByteBuffer
 * Package:  com.atzongmaotan.nio
 * Description:
 *
 * @Author: interface
 * @Create: 2024/9/30 - 上午9:40
 * @Version: v1.0
 */
public class BasicMappedByteBuffer {
    public static void main(String[] args) throws Exception {
        mappedByteBufferWrite();
    }
    private static void mappedByteBufferRead() throws IOException {

    }

    private static void mappedByteBufferWrite() throws IOException {
        RandomAccessFile raf = new RandomAccessFile("","rw");
        //获取Channel
        FileChannel channel = raf.getChannel();
        //创建读写模式的MappedByteBuffer,从position开始读写,只能修改size长度
        MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
        mappedByteBuffer.put(0, (byte) 'A');
        mappedByteBuffer.put(2, (byte) 'B');
        raf.close();
    }
}
